import Toast from 'tdesign-miniprogram/toast/index';
import { get } from '../../../../../utils/request';

Component({
  externalClasses: ['custom-class'],

  properties: {
    category: {
      type: Array,
    },
    initActive: {
      type: Array,
      value: [],
      observer(newVal, oldVal) {
        if (newVal[0] !== oldVal[0]) {
          this.setActiveKey(newVal[0], 0);
        }
      },
    },
    isSlotRight: {
      type: Boolean,
      value: false,
    },
    level: {
      type: Number,
      value: 3,
    },
  },
  data: {
    activeKey: 0,
    subActiveKey: 0,
    activeValues: [0],
    docList: [
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
      {
        name: '张三',
        level: '主治医生',
        type: '皮肤科',
        tag: ['顶尖专家', '公立医生'],
      },
    ],
    confirmBtn: { content: '确定预约', variant: 'base' },
    dialogKey: '',
    showConfirm: false,
    allDoc: [],
    currentDocList: [],
  },
  attached() {
    wx.request({
      url: `http://8.130.99.108:8090/api/getDoctors`, //仅为示例，并非真实的接口地址
      data: {},
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: 'GET',
      success: (res) => {
        if (res.data.message == '服务运行成功') {
          this.setData({
            allDoc: res.data.data,
            currentDocList: res.data.data[0],
          });
        }
      },
      fail: (error) => {
        console.log(error);
      },
    });
  },
  methods: {
    showDialog(e) {
      const { key, item } = e.currentTarget.dataset;
      console.log(item);
      this.setData({ [key]: true, dialogKey: key });
    },
    toDocDetail(e) {
      const { key, item } = e.currentTarget.dataset;
      console.log(item);
      wx.navigateTo({
        url: `/pages/docDetail/docDetail?item=${JSON.stringify(item)}`,
      });
    },

    closeDialog() {
      const { dialogKey } = this.data;
      this.setData({ [dialogKey]: false });
    },
    confirmPre() {
      const { dialogKey } = this.data;
      this.setData({ [dialogKey]: false });
      Toast({
        context: this,
        selector: '#t-toast',
        message: '预约成功,请前往预约记录查看',
      });
    },
    handleChange(e) {
      this.setData({
        activeValues: e.detail.value,
      });
    },

    onParentChange(event) {
      const activeKey = event.detail.index;
      const currentItem = this.data.category[activeKey];
      console.log(currentItem);

      const { allDoc } = this.data;
      let index = this.data.allDoc.findIndex((item) => item.departmentName == currentItem.name);
      console.log(allDoc[index]);
      const list = allDoc[index];
      this.setData({
        currentDocList: list,
      });
    },

    onChildChange(event) {},
  },
});
